//
//  DDAlertView.m
//  DingDongXiChe
//
//  Created by wtjr on 2017/11/22.
//  Copyright © 2017年 xieyingze. All rights reserved.
//

#import "DDAlertView.h"
#import "AppHeader.h"

@interface DDAlertView() {
    CGFloat _mainViewWidth;
    CGFloat _mainViewHeight;
    CGFloat _contensHeight;
}
@property (nonatomic, strong) UIView *backView;

@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) BaseButton *ensureButton;
@property (nonatomic, strong) BaseButton *cancelButton;

@property (nonatomic, strong) BaseLabel *titleLabel;
@property (nonatomic, strong) BaseLabel *contentsLabel;

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, copy) NSString *ensureButtonTitle;
@property (nonatomic, copy) NSString *cancelButtonTitle;

@end

@implementation DDAlertView

-(instancetype)initWithTitle:(NSString *)title content:(NSString *)content ensureButtonTitle:(NSString *)ensureButtonTitle cancelButtonTitle:(NSString *)cancelButtonTitle {
    if ([[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject] isKindOfClass:[DDAlertView class]]) return nil;
    self = [super init];
    if (self) {
        self.title = title;
        self.content = content;
        self.ensureButtonTitle = ensureButtonTitle;
        self.cancelButtonTitle = cancelButtonTitle;
        [self initData];
        [self initSubViews];
        [self autoLayoutSubViews];
    }
    return self;
}

#pragma mark - BaseMethod

- (void)initData {
    _mainViewWidth = 280 * kUI_Percent;
    _mainViewHeight = _mainViewWidth * 0.8;
    _contensHeight = [UILabel labelSpaceHeight:_content width:(_mainViewWidth - 20 * 2) font:16];
}

-(void)initSubViews {
    [self addSubview:self.backView];
    [self.backView addSubview:self.mainView];
    [self.mainView addSubview:self.titleLabel];
    [self.mainView addSubview:self.contentsLabel];
    [self.mainView addSubview:self.ensureButton];
    [self.mainView addSubview:self.cancelButton];
}

- (void)autoLayoutSubViews {
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(@0);
        make.width.equalTo(@(_mainViewWidth));
        make.height.equalTo(@(_mainViewHeight));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@20);
        make.centerX.equalTo(@0);
        make.height.equalTo(@20);
    }];
    [self.contentsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.bottom.equalTo(@-60);
    }];
    if (!self.cancelButtonTitle.length || self.cancelButtonTitle == nil) {
        self.cancelButtonTitle = @"取消";
    }
    UIView *xLine = [UIView new];
    xLine.backgroundColor = kCOLOR_line;
    [_mainView addSubview:xLine];
    [xLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.right.equalTo(@-5);
        make.top.equalTo(self.contentsLabel.mas_bottom);
        make.height.equalTo(@0.5);
    }];
    [self.ensureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(@0);
        make.top.equalTo(xLine.mas_bottom);
        make.width.equalTo(@(_mainViewWidth/2));
    }];
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(@0);
        make.top.equalTo(xLine.mas_bottom);
        make.width.equalTo(self.ensureButton);
    }];
    UIView *yLine = [UIView new];
    yLine.backgroundColor = kCOLOR_line;
    [_mainView addSubview:yLine];
    [yLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.width.equalTo(@0.3);
        make.top.equalTo(xLine.mas_bottom).offset(5);
        make.bottom.equalTo(@-5);
    }];
    if (!self.ensureButtonTitle.length || self.ensureButtonTitle == nil) {
        [self.ensureButton removeFromSuperview];
        [yLine removeFromSuperview];
        [self.cancelButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(_mainViewWidth));
        }];
    }
}

#pragma mark - Interface
/**
 跳出弹框
 */
- (void)show {
    [kUI_Window addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];

    [UIView animateWithDuration:0.25 animations:^{
        self.backView.backgroundColor = [kCOLOR_black colorWithAlphaComponent:0.3];
    }];
    
    [self.mainView.layer layerSpringPopWithName:kLayerScaleXY from:[NSValue valueWithCGSize:CGSizeMake(0.3f, 0.3f)] to:[NSValue valueWithCGSize:CGSizeMake(1.0f, 1.0f)] bounce:10 speed:30 key:nil complete:nil];
}

/**
 弹出弹框及消失
 */
- (void)showAndDismissSoon {
    [kUI_Window addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.backView.backgroundColor = [kCOLOR_black colorWithAlphaComponent:0.3];
    }];
    
    [self.mainView.layer layerSpringPopWithName:kLayerScaleXY from:[NSValue valueWithCGSize:CGSizeMake(0.3f, 0.3f)] to:[NSValue valueWithCGSize:CGSizeMake(1.0f, 1.0f)] bounce:10 speed:30 key:nil complete:nil];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hide:self.cancelButton];
    });
}

/**
 隐藏弹框
 */
- (void)hide:(BaseButton *)btn {
    if (self.clickeCompleted) {
        self.clickeCompleted(btn.tag);
    }
    [self.mainView.layer layerBasicPopWithName:kLayerScaleXY to:[NSValue valueWithCGSize:CGSizeMake(0.01f, 0.01f)] duration:0.25 key:nil complete:nil];
    [UIView animateWithDuration:0.25 animations:^{
        self.backView.backgroundColor = [kCOLOR_black colorWithAlphaComponent:0.01];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - Delegate


#pragma mark - TouchEvent

- (void)clickEnsureButton:(BaseButton *)btn {
    [self hide:btn];
}

#pragma mark - Setter


#pragma mark - Getter

- (UIView *)backView {
    if (!_backView) {
        _backView = [UIView new];
        _backView.backgroundColor = [kCOLOR_black colorWithAlphaComponent:0.01];
    }
    return _backView;
}

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = kCOLOR_white;
        _mainView.layer.cornerRadius = kUI_LayerCorner;
    }
    return _mainView;
}

- (BaseButton *)ensureButton {
    if (!_ensureButton) {
        _ensureButton = [BaseButton new];
        _ensureButton.tag = 1;
        [_ensureButton setButtonStyle:@"确定" titleColor:KCOLOR_mainColor titleFont:kFONT_mainFont(16) texrAlignment:NSTextAlignmentCenter];
        [_ensureButton addTarget:self action:@selector(clickEnsureButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _ensureButton;
}

- (BaseButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [BaseButton new];
        _cancelButton.tag = 0;
        [_cancelButton setButtonStyle:@"取消" titleColor:KCOLOR_font_tint titleFont:kFONT_mainFont(16) texrAlignment:NSTextAlignmentCenter];
        [_cancelButton addTarget:self action:@selector(hide:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (BaseLabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [BaseLabel new];
        [_titleLabel setLabelStyle:self.title textColor:KCOLOR_mainColor textFont:kFONT_mainFont(20) texrAlignment:NSTextAlignmentCenter];
    }
    return _titleLabel;
}

- (BaseLabel *)contentsLabel {
    if (!_contentsLabel) {
        _contentsLabel = [BaseLabel new];
        [_contentsLabel setLabelStyle:self.content textColor:KCOLOR_font_normal textFont:kFONT_mainFont(14) texrAlignment:NSTextAlignmentCenter];
    }
    return _contentsLabel;
}

@end
